<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>驱动管理</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}" media="screen"/>
    <link rel="stylesheet" th:href="@{/static/css/layui.css}" media="all"/>
</head>
<body>
<form class="layui-form">
    <div class="layui-panel">
        <div style="margin: 15px;">
            <div class="layui-form-item">
                <label class="layui-form-label">驱动名称:</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" placeholder="请输入驱动名称" class="layui-input"
                           lay-affix="clear">
                </div>
                <label class="layui-form-label">类型:</label>
                <div class="layui-input-inline">
                    <label>
                        <select name="typeName">
                            <option value=""></option>
                            <option value="内置">内置</option>
                            <option value="自定义">自定义</option>
                        </select>
                    </label>
                </div>
                <div class="layui-input-normal">
                    <button class="layui-btn layui-btn-primary layui-border-green search-btn" lay-submit id="search"
                            lay-filter="table-search">
                        <i class="layui-icon layui-icon-search"> </i>查 询
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-blue"
                            onclick="openPage('新增用户',ctx+'settingManager/addDriverConfigPage')
                            return false;">
                        <i class="layui-icon layui-icon-add-1">&nbsp;</i>添 加
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-panel" style="margin-top: 15px;">
        <div class="layui-form nowrap">
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</form>
</body>
<script th:src="@{/static/layui.js}"></script>
<script th:replace="~{base::ctx}"/>
<script type="text/html" id="toolbarTable">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="update">修改</a>
        <a class="layui-btn layui-bg-red layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>
<script>
    layui.use(function () {
        const layer = layui.layer;
        const form = layui.form;
        const $ = layui.jquery;
        const table = layui.table;

        table.render({
            elem: '#dataTable',
            url: ctx + 'settingManager/driverConfigList',
            toolbar: true,
            defaultToolbar: ['filter', 'exports', 'print'],
            page: true,
            height: '525',
            cols: [
                [
                    {field: 'name', title: '驱动名称', sort: true, fixed: true},
                    {field: 'driverClass', title: '驱动类', sort: true, fixed: true, width: 400},
                    {field: 'url', title: '默认地址', sort: true, fixed: true, width: 400},
                    {field: 'dbCheckUrl', title: '检查有效性', sort: true, fixed: true},
                    {field: 'typeName', title: '类型', sort: true, fixed: true},
                    {fixed: 'right', title: '操作', width: 200, toolbar: '#toolbarTable'}
                ]
            ],
            parseData: res => parseData(res)
        });

        table.on('tool(dataTable)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'update':
                    if (data.typeName === "内置") {
                        layer.msg("内置驱动修改不会影响已创建的数据源!", {icon: 2});
                        //return false;
                    }
                    openPage("修改驱动信息", ctx + "settingManager/addDriverConfigPage?id=" + data.id);
                    break;
                case 'delete':
                    if (data.typeName === "内置") {
                        layer.msg("内置驱动不允许删除!", {icon: 2});
                        return false;
                    }
                    layer.confirm('您确认要删除此驱动吗?', {icon: 3}, function () {
                        post(ctx + "settingManager/deleteDriverConfig/" + data.id, null, (res) => {
                            layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                                $(".search-btn").click();
                            });
                        })
                    }, function () {
                    });
                    break;
            }
        });

        form.on('submit(table-search)', function (data) {
            const field = data.field;
            table.reloadData('dataTable', {
                scrollPos: true,
                where: field
            });
            return false;
        });
    });
</script>
</html>